﻿using System;
using System.Collections;//
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form13 : Form
    {
        public Form13()
        {
            InitializeComponent();
        }
        
        private void Form13_Load(object sender, EventArgs e)
        {
            #region 13-01
            CodeImage(CheckCode());
            #endregion
        }
        #region 13-01
        private string CheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;
            Random random = new Random();
            for(int i = 0; i < 4; i++)
            {
                number = random.Next();
                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));
                checkCode += " " + code.ToString();  
            }
            return checkCode;
        }

        private void CodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 9.5)), 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                Random random = new Random();
                g.Clear(Color.White);
                for(int i = 0; i < 3; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Black), x1, x2, y1, y2);
                }
                Font font = new Font("Arial", 12, (FontStyle.Bold));
                g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
                for(int i = 0; i < 150; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                this.pictureBox1.Width = image.Width;
                this.pictureBox1.Height = image.Height;
                this.pictureBox1.BackgroundImage = image;
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CodeImage(CheckCode());
        }
        #endregion

        #region 13-02
        static int SumNum;
        static float TimeNum;
        SqlConnection con;
        SqlCommand cmd;
        Hashtable ht = new Hashtable();
        private void Conn()
        {
           // con = new SqlConnection("server=XIAOKE;uid=sa;pwd =111111;database=db_EMS");//无例子中数据库 GG
            //con.Open;
        }
        #endregion

        #region 13-03
        private void Form13_Paint(object sender, PaintEventArgs e)
        {
            Image img = Image.FromFile("1.png");
            Graphics g = this.CreateGraphics();
            g.DrawImage(img, 100, 100, 100, 100);
        }
        #endregion

        private void groupBox3_Paint(object sender, PaintEventArgs e)
        {
        }
    }
}
